﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemSettings.InstituTions;
using SystemCenterSoa.IRepository.SystemSettings.InstituTions;
using SystemCenterSoa.PoModel.SystemSettings.InstituTions;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSettings.InstituTions
{
    public class QualificationManagementRepository : BaseRepository,IQualificationManagementRepository
    {
       public QualificationManagementRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }

        public bool QuaAdd(QualificationManagementPo info)
        {
            _systemCenterDbSoaContext.QualificationManagementPo.Add(info);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public QualificationManagementPo QuaEdit(int id)
        {
            return _systemCenterDbSoaContext.QualificationManagementPo.Find(id);
        }

        public bool QuaUpdate(QualificationManagementPo info)
        {
            _systemCenterDbSoaContext.Entry(info).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public bool QueDelete(string ids)
        {
            string[] arr = ids.Split(',');
            foreach (var item in arr)
            {
                int id = Convert.ToInt32(item);
                var list = _systemCenterDbSoaContext.QualificationManagementPo.Find(id);
                _systemCenterDbSoaContext.QualificationManagementPo.Remove(list);
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public PagerResult<QualificationManagementPo> SearchPage(SearchModel searchModel)
        {
            var query = _systemCenterDbSoaContext.QualificationManagementPo.AsQueryable();
            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                Dictionary<string, string> tempDi = searchModel.SearchItem;

                //tempDi["user_name"]
                //搜索字段为user_name  
                string name = "name";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    query = query.Where(c => c.qua_manager_name.Contains(tempDi[name]));
                }
                

                //user_status
                string levelid = "levelid";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(levelid) && !string.IsNullOrEmpty(tempDi[levelid]))
                {
                    query = query.Where(c => c.qua_manager_level.Equals(tempDi[levelid]));
                }


                //begin_creator_date  创建时间时间段查询的开始时间
                string reviewdate = "reviewdate";
                //判断搜索条件中是否有user_status
                if (tempDi.Keys.Contains(reviewdate) && !string.IsNullOrEmpty(tempDi[reviewdate]))
                {
                    DateTime tempCreatorDate = new DateTime();
                    if (DateTime.TryParse(tempDi[reviewdate], out tempCreatorDate))
                    {
                        query = query.Where(c => c.qua_review_date >= tempCreatorDate);
                    }

                }
               
            }
            return BasePageSearch(query, searchModel);
        }
    }
}